package io.renren.modules.doc.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.doc.entity.DocumentCategoryEntity;
import io.renren.modules.doc.service.DocumentCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 文档分类管理
 */
@RestController
@RequestMapping("/doc/category")
public class DocumentCategoryController {
    @Autowired
    private DocumentCategoryService documentCategoryService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("doc:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = documentCategoryService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 分类树
     */
    @GetMapping("/tree")
    @RequiresPermissions("doc:category:list")
    public R tree(){
        List<DocumentCategoryEntity> tree = documentCategoryService.getCategoryTree();
        return R.ok().put("tree", tree);
    }

    /**
     * 获取子分类
     */
    @GetMapping("/children/{parentId}")
    @RequiresPermissions("doc:category:list")
    public R children(@PathVariable("parentId") Long parentId){
        List<DocumentCategoryEntity> children = documentCategoryService.getChildren(parentId);
        return R.ok().put("children", children);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{categoryId}")
    @RequiresPermissions("doc:category:info")
    public R info(@PathVariable("categoryId") Long categoryId){
        DocumentCategoryEntity category = documentCategoryService.getById(categoryId);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("doc:category:save")
    public R save(@RequestBody DocumentCategoryEntity category){
        documentCategoryService.saveCategory(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("doc:category:update")
    public R update(@RequestBody DocumentCategoryEntity category){
        documentCategoryService.updateCategory(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("doc:category:delete")
    public R delete(@RequestBody Long[] ids){
        documentCategoryService.deleteCategory(ids);
        return R.ok();
    }
}
